//
//  WPSexPickerView.m
//  wobingwoyi
//
//  Created by WGP on 16/8/3.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "WPSexPickerView.h"
#import "HMKeyboardTool.h"

@interface WPSexPickerView ()<UIPickerViewDataSource,UIPickerViewDelegate>

// 工具条
@property (nonatomic, weak) HMKeyboardTool *tool;
// 日期选择器
@property (nonatomic, weak) UIPickerView *sexPicker;
// 遮盖按钮
@property (nonatomic, strong) UIButton *coverBtn;

@property(nonatomic,copy)void(^handleBlock)(NSString *sex);
@property(nonatomic,copy)NSString *selectedSex;

@end
@implementation WPSexPickerView

+(instancetype)sexPickerViewWithHandle:(void(^)(NSString *sex))handleBlock
{
     return [[self alloc] initWithHandleBlock:handleBlock];
}

- (instancetype)initWithHandleBlock:(void(^)(NSString *sex))handleBlock
{
    if (self = [super init])
    {
        self.handleBlock = handleBlock;
        self.backgroundColor = [UIColor whiteColor];
        [self setup];
    }
    return self;
}


/**
 *  添加子控件
 */
- (void)setup {
    
    self.selectedSex = @"男";
    
    // 创建工具条
    HMKeyboardTool *tool = [HMKeyboardTool tool];
    // 设置block
    tool.btnClickBlock = ^(HMKeyboardToolButtonType buttonType){
        // 隐藏当前view
        [self dismiss];
        if (buttonType == HMKeyboardToolButtonTypeSure)
        {
            self.handleBlock(self.selectedSex);
        }
        
    };
    // 将工具条添加到当前view
    [self addSubview:tool];
    self.tool = tool;
    
    // 创建日期选择器
    UIPickerView *sexPicker = [[UIPickerView alloc] init];
    sexPicker.delegate = self;
    self.sexPicker = sexPicker;

    // 添加到当前view
    [self addSubview:sexPicker];
    
    [self.tool mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(40);
    }];
    
    [self.sexPicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.top.equalTo(self.tool.mas_bottom);
        make.bottom.equalTo(self);
    }];
}

#pragma mark - UIPickerView 数据源方法
/**
 *  告诉pickerView有多少列数据
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}
/**
 * 告诉pickerView第component列有多少行数据
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return 2;
}

#pragma mark - UIPickerView 代理方法
/**
 *  告诉pickerView第component列第row行显示怎样的字符串
 当有一行数据出现在视野范围内时调用
 *
 */
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (row == 0)
    {
        return @"男";
    }
    return @"女";
}


/**
 *  监听pickerView的选择事件
 *  @param row        选中的行号
 *  @param component  列号
 */
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if (row == 0)
    {
        self.selectedSex = @"男";
    }
    self.selectedSex = @"女";
}


#pragma mark - 显示和隐藏方法
/**
 *  显示
 */
- (void)show {
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGFloat screenH = CGRectGetHeight(window.frame);
    CGFloat screenW = CGRectGetWidth(window.frame);
    
    // 创建遮盖按钮
    self.coverBtn = [[UIButton alloc] initWithFrame:window.bounds];
    self.coverBtn.backgroundColor = RGBA_COLOR(51, 51, 51, 0.5);
    [self.coverBtn addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
    // 将按钮添加到窗口上
    [window addSubview:self.coverBtn];
    
    // 设置当前view的frame
    CGFloat height = 256*K_SCALE_FULL;
    self.frame = CGRectMake(0, screenH, screenW, height);
    
    // 将当前view添加到窗口上
    [window addSubview:self];
    
    // 以动画的形式显示出来
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = screenH - height;
        self.frame = frame;
    }];
}
/**
 *  隐藏
 */
- (void)dismiss{
    // 获得应用程序主窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    CGFloat screenH = CGRectGetHeight(window.frame);
    
    // 将遮盖按钮移除
    [self.coverBtn removeFromSuperview];
    self.coverBtn = nil;

    
    // 以动画的形式隐藏日期选择器
    [UIView animateWithDuration:0.25 animations:^{
        CGRect frame = self.frame;
        frame.origin.y = screenH;
        self.frame = frame;
        
    } completion:^(BOOL finished) {
        // 移除当前控件
        [self removeFromSuperview];
    }];
}
@end






